<?php

namespace App\Services\Core\Traits;

trait Data
{


    public function filterNull(array $data = [])
    {
        foreach ($data as $field => &$item) {
            if (null === $item) unset($data[$field]);
        }
        return $data;
    }

    public function listToTree(array $list, string $pid = 'pid', string $pk = 'id', string $child = 'children', int $paretnLv = 0)
    {
        $tree = array();
        if (is_array($list)) {
            $refer = array();
            foreach ($list as $key => $data) $refer[$data[$pk]] =& $list[$key];

            foreach ($list as $key => $data) {
                $parentId = $data[$pid];

                if ($paretnLv == $parentId) {
                    $tree[] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent           =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }


}
